/**
 * 
 */
package com.itboye.jmsfhz.app;

import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap.Config;
import android.os.Build;
import android.os.Vibrator;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.baidu.mapapi.SDKInitializer;
import com.itboye.baidu.util.LocationService;
import com.itboye.baidu.util.TipsToast;
import com.itboye.core.server.ByService;
import com.itboye.jmsfhz.R;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

/**
 * @author hebidu
 * 
 */
public class MainApplication extends Application {
	private static TipsToast tipsToast;
	public static Context context;

	private static RequestQueue queue;

	private static MainApplication getInstance;

	public static LocationService locationService;
	public Vibrator mVibrator;

	public static MainApplication getGetInstance() {
		return getInstance;
	}

	public void initImageLoader() {
		long maxMamory = Runtime.getRuntime().maxMemory();// 应用程序程序运行时所使用最大内存数
		maxMamory = (long) (maxMamory * 0.7);
		int v = Runtime.getRuntime().availableProcessors();
		int availableProcessors = v < 4 ? 3 : v - 1;

		// System.out.println("maxMamory : " + maxMamory +
		// "  availableProcessors: " + availableProcessors);

		// DebugToast.show("maxMamory : " + (maxMamory/1024.0f/1024 )+
		// "  availableProcessors: " + availableProcessors, 1);

		DisplayImageOptions options = new DisplayImageOptions.Builder()
				// 设置图片下载期间显示的图片
				.showImageOnLoading(R.drawable.moren)
				.showImageForEmptyUri(R.drawable.moren)// 设置图片Uri为空或是错误的时候显示的图片
				.showImageOnFail(R.drawable.moren) // 设置图片加载或解码过程中发生错误显示的图片
				.cacheInMemory(true)// 设置下载的图片是否缓存在内存中
				.cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
				.bitmapConfig(Config.RGB_565)
				// .displayer(new RoundedBitmapDisplayer(20))// 设置成圆角图片
				.build(); // 创建配置过得DisplayImageOption对象 

		// File cacheDir = StorageUtils.getOwnCacheDirectory(App.ctx,
		// "sunsun/Cache");
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getInstance)
				.memoryCacheExtraOptions(480, 800)
				// max width, max
				// height，即保存的每个缓存文件的最大长宽
				// .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75,
				// null) // Can slow ImageLoader, use it carefully (Better don't
				// use it)/设置缓存的详细信息，最好不要设置这个
				.threadPoolSize(availableProcessors)
				// 线程池内加载的数量
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				// .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 *
				// 1024)) // You can pass your own memory cache
				// implementation/你可以通过自己的内存缓存实现
				// .memoryCache(new WeakMemoryCache())

				.memoryCacheSize((int) maxMamory)
				// .memoryCacheSize(15 * 1024 * 1024)

				.diskCacheSize(80 * 1024 * 1024)
				.diskCacheFileNameGenerator(new Md5FileNameGenerator())// 将保存的时候的URI名称用MD5
																		// 加密

				.tasksProcessingOrder(QueueProcessingType.LIFO) // 后进先出
				// .diskCacheFileCount(100) //缓存的文件数量
				// .diskCache(new UnlimitedDiscCache(cacheDir))//自定义缓存路径
				// .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
				.defaultDisplayImageOptions(options)
				// .imageDownloader(new BaseImageDownloader(App.ctx, 5 * 1000,
				// 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
				// .imageDownloader(new DownloadImageFromNetWork(App.ctx, 5 *
				// 1000, 30 * 1000)).writeDebugLogs() // Remove
				// for
				// release
				// app
				.build();// 开始构建
		// Initialize ImageLoader with configuration.

		com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(
				config);// 全局初始化此配置

	}

	public static void setGetInstance(MainApplication getInstance) {
		MainApplication.getInstance = getInstance;
	}

	@Override
	public void onCreate() {
		super.onCreate();

		locationService = new LocationService(getApplicationContext());
		mVibrator = (Vibrator) getApplicationContext().getSystemService(
				Service.VIBRATOR_SERVICE);
		
		SDKInitializer.initialize(getApplicationContext());
		getInstance = this;
		context = getApplicationContext();
		initImageLoader();
		Intent intent = new Intent(getApplicationContext(), ByService.class);
		// intent.putExtra("msg", "asdsdas");
		startService(intent);
		queue = Volley.newRequestQueue(context);
	}

	public static Context getContext() {

		return context;
	}

	/**
	 * @return the queue
	 */
	public static RequestQueue getQueue() {
		return queue;
	}

	public static <T> void addRequest(Request<T> request) {
		request.addMarker("itboye");
		getQueue().add(request);
	}

	
	
}
